package com.yeban.base.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * @Desc TODO
 * @Author yeban
 * @Date 2025/10/2
 */

// 五子棋
public class Gobang {
    List<List<String>> res = new ArrayList<>();
    public char[] letter;
    public char[][] chessBoard;

    public boolean isBlackRound;    //回合
    public int counts;  //一共下了几个回合
    public int x;   // x坐标
    public int y;   // y坐标

    // 游戏
    public void startGame() {

        startChessBoard();          // 初始化棋盘
        printChessBoard();          // 打印棋盘
        counts = 0;                 // 回合数初始化

        while (true) {

            // 判断回合
            isBlackRound = (counts % 2 == 0);
            char piece = isBlackRound ? '@' : 'x';
            String player = isBlackRound ? "黑方" : "白方";

            // 下棋
            playChess(piece, player);
            counts ++;

            // 胜利
            if(isWinner(x, y, piece)) {
                System.out.println("游戏结束，" + player + "获胜！");
                System.out.println("总回合数:" + counts);
                break;
            }

            // 平局
            if(counts == chessBoard.length * chessBoard.length) {
                System.out.println("棋盘已满！双方平局！");
                break;
            }
        }
    }

    // 初始化棋盘
    public void startChessBoard() {

        // 输入棋盘大小
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入棋盘的大小，至少为5：");
        int n = sc.nextInt();

        letter = new char[n];
        chessBoard = new char[n][n];

        // 初始化棋盘
        for (int i = 0; i < n; i++) {
            letter[i] = (char) ('a' + i);
            for (int j = 0; j < n; j++) {
                chessBoard[i][j] = '*';
            }
        }

        // 提示即将开局
        System.out.println("即将开局");

    }

    // 打印棋盘
    public void printChessBoard() {

        // 棋盘长度
        int n = chessBoard.length;

        for (int i = 0; i < n; i++) {
            System.out.print(letter[i] + "  ");
            for (int j = 0; j < n; j++) {
                System.out.print(chessBoard[i][j] + "  ");
            }
            System.out.println();
        }

        for (int i = 0; i <= n; i++) {
            if (i == 0) {
                System.out.print(" ");
            } else {
                System.out.print("  " + letter[i - 1]);
            }
        }
        System.out.println();
    }

    // 坐标转化
    public int xyTurn(char xy) {

        for (int i = 0; i < chessBoard.length; i++) {
            if(letter[i] == xy) {
                return i;
            }
        }
        return -1;
    }

    // 黑方下棋：@  白棋下棋：x
    public void playChess(char piece, String player) {

        Scanner sc = new Scanner(System.in);

        while(true) {
            System.out.println("\n请"+ player + "下子：");

            //输入棋子字母坐标，转化成数字坐标
            String xy = sc.next();
            x = xyTurn(xy.charAt(0));
            y = xyTurn(xy.charAt(1));

            // 判断下的棋子是否合理，并下棋
            if(isTrue(x,y)) {
                chessBoard[x][y] = piece;     // 下棋
                printChessBoard();            // 打印棋盘
                break;  // 当前回合结束
            }
        }
    }

    // 判断下的子是否合法
    public boolean isTrue(int x, int y) {
        // 下的棋子是否越界
        if(x == -1 || y == -1) {
            System.out.println("不在棋盘内！请重新选择位置！");
            return false;
        }

        // 是否被占用
        if(chessBoard[x][y] == '@' || chessBoard[x][y] == 'x') {
            System.out.println("不能下在下过的地方！请重新选择位置！");
            return false;
        }
        return true;
    }

    // 判断是否胜利
    public boolean isWinner(int x, int y, char piece) {

        return isWinner01(x,y,piece) || isWinner02(x,y,piece) || isWinner03(x,y,piece) || isWinner04(x,y,piece);

    }

    // 竖直方向
    public Boolean isWinner01(int x, int y, char piece){

        // 设置边界
        int top = 0;   // 上边界
        int bottom = 0;  // 下边界

        String s = "";


        // 遍历是否五子连子
        for (int i = 1; i < 5; i++) { // 向上
            if (x - i < 0 || chessBoard[x - i][y] != piece) break;
            top ++;
        }
        for (int i = 1; i < 5; i++) { // 向下
            if (x + i >= chessBoard.length || chessBoard[x + i][y] != piece) break;
            bottom ++;
        }

        // 判断黑方是否胜利
        if(top + 1 + bottom >= 5) {
            return true;
        }
        return false;

    }

    // 左下右上方向
    public Boolean isWinner02(int x, int y ,char piece){

        // 设置边界
        int leftBottom = 0;  // 左下边界
        int rightTop = 0;   // 右上边界

        // 遍历是否五子连子
        for (int i = 1; i < 5; i++) { // 右上
            if(x - i < 0 || y + i >= chessBoard.length || chessBoard[x - i][y + i] != piece) break;
            rightTop ++;
        }

        for (int i = 1; i < 5; i++) {   // 左下
            if(x + i >= chessBoard.length || y - i < 0 || chessBoard[x + i][y - i] != piece) break;
            leftBottom ++;
        }

        if(rightTop + 1 + leftBottom >= 5) {
            return true;
        }
        return false;

    }

    // 水平方向
    public Boolean isWinner03(int x, int y, char piece){
        int left = 0;   // 左边界
        int right = 0;  // 右边界

        for (int i = 1; i < 5; i++) {   // 向左
            if(y - i < 0 || chessBoard[x][y - i] != piece) break;
            left ++;
        }

        for (int i = 1; i < 5; i++) {   // 向右
            if(y + i >= chessBoard.length || chessBoard[x][y + i] != piece) break;
            right ++;
        }

        if(left + 1 + right >= 5) {
            return true;
        }
        return false;

    }

    // 左上右下方向
    public Boolean isWinner04(int x, int y, char piece){

        int leftTop = 0;  // 左上边界
        int rightBottom = 0;   // 右下边界

        for (int i = 1; i < 5; i++) {   // 左上
            if(x - i < 0 || y - i < 0 || chessBoard[x - i][y - i] != piece) break;
            leftTop ++;
        }

        for (int i = 1; i < 5; i++) {   // 右下
            if(x + i >= chessBoard.length || y + i >= chessBoard.length || chessBoard[x + i][y + i] != piece) break;
            rightBottom ++;
        }

        if(leftTop + 1 + rightBottom >= 5) {
            return true;
        }
        return false;

    }

    public static void main(String[] args) {

        Gobang gobang = new Gobang();
        gobang.startGame();

    }
}





